import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/custom_filter_right_sheet.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/core/values/colors.dart';
import 'package:enterprise_v6_app/app/core/values/dimens.dart';
import 'package:enterprise_v6_app/app/core/values/gaps.dart';
import 'package:enterprise_v6_app/app/core/values/images.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/major_hazard_information_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/basic_information/productive_process/major_hazard_base/major_hazard_base_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';

///重大危险源页面
class MajorHazardBasePage extends StatelessWidget {
  MajorHazardBasePage({super.key});

  final controller = Get.put(MajorHazardBaseController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            appBar: CustomAppBar(
              titleText: '重大危险源',
            ),
            key: controller.popRightScaffoldKey,
            // 右侧筛选栏
            endDrawer: Container(
              width: AppDimens.sw - 70.w,
              height: AppDimens.sh,
              color: Colors.white,
              child: _getFilterWidget(),
            ),
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: _getSearchEnterWidget(),
                      ),
                      AppGaps.hGap8,
                      _screen(),
                      AppGaps.vGap10,
                    ],
                  ),
                  AppGaps.vGap12,
                  Expanded(
                      child: CustomEasyRefreshList<MajorHazardInformationEntity>(
                    key: controller.globalKey,
                    dataLoad: (pageNum, pageSize, {search}) {
                      return controller.getListData(pageNum, pageSize);
                    },
                    itemBuilder: (item, index) {
                      return CustomTitleChartPage(
                        title: item.riskObjectName!,
                        titleRightWidget: _getResult(item.hazardRank!),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            _rowData('分类：', item.hazardTypeLabel!),
                            AppGaps.vGap12,
                            _rowData('是否在化工园区内：', item.inChemicalIndustrialPark! == 0 ? '是' : '否'),
                            AppGaps.vGap12,
                            _rowData('所属化工园区：', item.parkName!),
                          ],
                        ),
                      ).clickOnce(() {
                        Get.toNamed(AppRoutes.majorHazardBaseItemDetail, arguments: item.id!);
                      });
                    },
                  )),
                ],
              ),
            ), // 右侧筛选栏
          );
        });
  }

  ///获取右侧筛选样式
  Widget _getFilterWidget() {
    return CustomFilterRightSheet(
      dataList: controller.filterList,
      onConfirm: controller.screenConfirm,
      isRadio: true,
    );
  }

  //搜索框
  Widget _getSearchEnterWidget() {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    onChanged: (value) {
                      controller.search = value;
                      controller.refreshPage();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '请输入危险源名称/园区名称',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 38.w,
      height: 38.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).click(controller.onFilterButtonPressed);
  }

  ///危险等级（1：一级；2：二级；3：三级；4：四级；）
  Widget _getResult(String i) {
    switch (i) {
      case '1':
        return _titleImage(AppImages.major_hazard_level_one);
      case '2':
        return _titleImage(AppImages.major_hazard_level_two);
      case '3':
        return _titleImage(AppImages.major_hazard_level_three);
      case '4':
        return _titleImage(AppImages.major_hazard_level_four);
      default:
        return Container();
    }
  }

  Widget _titleImage(String image) {
    return SvgPicture.asset(image);
  }

  Widget _rowData(String name, String value, {MainAxisAlignment mainAxisAlignment = MainAxisAlignment.spaceBetween}) {
    return Row(
      mainAxisAlignment: mainAxisAlignment,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Text(
          value,
          style: TextStyle(
            color: const Color(0xFF495061),
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        )
      ],
    );
  }
}
